<template>
    <view class="page-wrap">
        <Position :info="workDetail" :type="type" :remark="remark" @changeRemark="changeRemark"></Position>
        <view class="content">
            <view class="forms">
                <view class="item">
                    <view class="label">用户负责人</view>
                    <view class="right">
                        <u-input inputAlign="right" border="none" type="text" v-model="form['用户负责人']" placeholder="请输入" />
                    </view>
                </view>
                <view class="item">
                    <view class="label">联系电话</view>
                    <view class="right">
                        <u-input inputAlign="right" border="none" type="number" v-model="form['联系电话']" placeholder="请输入" />
                    </view>
                </view>
                <view class="item">
                    <view class="label">培训人员</view>
                    <view class="right">
                        <u-input inputAlign="right" border="none" type="text" v-model="form['培训人员']" placeholder="请输入" />
                    </view>
                </view>

                <view class="item">
                    <view class="label">培训类型</view>
                    <view class="right">
                        <u-radio-group v-model="form['培训类型']" placement="row">
                            <u-radio label="培训" name="培训"></u-radio>
                            <u-radio label="消防演练" name="消防演练"></u-radio>
                        </u-radio-group>
                    </view>
                </view>


                <view class="item">
                    <view class="label">参加演练人员</view>
                    <view class="right">
                        <u-input inputAlign="right" border="none" type="text" v-model="form['参加演练人员']" placeholder="请输入" />
                    </view>
                </view>




            </view>

            <view class="network" v-for="(key, index) in formKeys" :key="index">
                <view class="item-title">
                    <view class="title">
                        <view class="txt">
                            {{ key }}
                        </view>
                        <view class="bg"></view>
                    </view>
                    <ImgUpload v-if="form[key].needFile" :uploadParams="{ ...uploadParams, type_name: key }" :title="key"
                        :fileList="form[key].fileList" @change="(val) => fileChange(val, key)">
                    </ImgUpload>
                </view>
                <view class="child">
                    <textarea :maxlength="-1"  v-model="form[key].value" class="my-textarea" placeholder="请输入"></textarea>
                </view>

            </view>

            <view class="h1" style="height: 1px ;"></view>
        </view>

        <view class="btns" v-if="!isDetail">
            <view class="btn save" @tap="submit(1)">存草稿</view>
            <view class="btn sub" @tap="submit(0)">确认提交</view>
        </view>
    </view>
</template>

<script>
import { patrolAdd, getWorkDetail, getWorkInfo } from '@/api'
export default {

    computed: {

        workType() {
            let map = {
                1: "网点",
                3: "安防",
                2: "基站",
            }
            return map[this.type]
        },

    },
    data() {
        return {
            isDetail: '',
            formKeys: ['培训方式', '培训演练内容', '培训演练情况记录'],
            form: {
                "用户负责人": '',
                '联系电话': '',
                '培训人员': '',
                '培训类型': '培训',
                '参加演练人员': '',
                '培训方式': {
                    fileList: [],
                    needFile: false,
                    value: ''
                }, '培训演练内容': {
                    fileList: [],
                    needFile: true,
                    value: ''
                }, '培训演练情况记录': {
                    fileList: [],
                    needFile: false,
                    value: ''
                }
            },
            remark: '',
            uploadParams: {},
            workInfo: null,
            workDetail: null,
            type: '1',
            defaultValue: null
        }
    },
    onLoad(options) {
        console.log(options)
        const { type, id, pid, mid, isDetail } = options
        this.type = type
        this.isDetail = isDetail
        this.workInfo = {
            type, id, pid, mid
        }
        this.uploadParams = {
            type, id, pid, mid,
            wid: this.wid
        }
        this.getWorkInfo(this.workInfo)
        this.getWorkDetail(this.workInfo)
    },
    onShow(){
        this.getWorkInfo(this.workInfo) 
    },
    methods: {
        async getWorkDetail(params) {
            if (params.id != '0') {
                let res = await getWorkDetail({
                    id: params.id,
                    wid: this.wid,
                    type: "4"
                })
                if (res.formdata) {
                    this.defaultValue = JSON.parse(res.formdata)
                    let keys=Object.keys(this.defaultValue)
                    if (this.defaultValue && keys.includes('备注')) {
                        this.remark = this.defaultValue['备注'] || '';
                        delete this.defaultValue['备注']
                    }
                }
            }
        },
        async getWorkInfo(params) {
            let res = await getWorkInfo({
                wid: this.wid,
                mid: params.mid
            })
            this.workDetail = res
        },
        fileChange(val, key = '') {
            this.form[key].fileList = val
        },
        getForm(val) {
            this.form = val;
        },
        submit(draft = 0) {
            wx.showModal({
                title: '提示',
                content: '是否确定提交全部工单和附件图片？',
                success: async (res) => {
                    if (res.confirm) {
                        console.log('用户点击确定', JSON.stringify(this.form))
                        let params = {
                            ...this.workInfo,
                            ac: 'train_add',
                            formdata: JSON.stringify({
                                ...this.form,
                                '备注': this.remark
                            }),
                            wid: this.wid,
                            draft
                        }
                        if (params.id == '0') {
                            delete params.id
                        }
                        await patrolAdd(params)
                        wx.showToast({
                            title: draft ? '保存至草稿成功' : '工单提交成功'
                        })
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1000)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    },
    watch: {
        defaultValue: {
            handler(val) {
                this.form = val
            },
        }
    }
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


